import datetime

import pandas as pd

from jili.tool import is_int4str
def data_dropna(data,subset=None):
    if isinstance(data,dict):
        rst={}
        for k,v in data.items():
            if isinstance(v,pd.DataFrame):
                rst[k]=v.dropna(subset=subset)
            else:
                rst[k] = v.dropna()
    elif isinstance(data,list):
        rst=[]
        for v in data:
            if isinstance(v,pd.DataFrame):
                k=v.dropna(subset=subset)
            else:
                k = v.dropna()
            rst.append(k)
    else:
        if isinstance(data, pd.DataFrame):
            rst = data.dropna(subset=subset)
        else:
            rst = data.dropna()
    return rst
def convert_datetime(a,segment="-"):
    if isinstance(a,str):
        return str2datetime(a,segment)
    else:
        return a
def get_timestr(timestr,segment=None):
    """
    Args:
        timestr:
`       8 11:11:11
        6 111111
        5 11:11
        4 1111
        2 11
    Returns:

    """
    if "." not in timestr:
        if len(timestr)==8:
            if segment:
                tt = segment
            else:
                tt = timestr[2]
            return "%H"+tt+"%M"+tt+"%S"
        elif len(timestr)==6:
            return "%H%M%S"
        elif len(timestr)==5:
            if segment:
                tt = segment
            else:
                tt = timestr[2]
            return "%H"+tt+"%M"
        elif len(timestr)==4:
            return "%H%M"
        elif len(timestr)<=2:
            return "%H"
    else:
        timestr=timestr.split(".")[0]
        if len(timestr)==8:
            return "%H"+tt+"%M"+tt+"%S.%f"
        elif len(timestr)==6:
            return "%H%M%S.%f"
        elif len(timestr)==5:
            return "%H"+tt+"%M.%f"
        elif len(timestr)==4:
            return "%H%M.%f"
        elif len(timestr)<=2:
            return "%H.%f"
    return None
def str2time(strdate,segment=None):
    if isinstance(strdate,datetime.datetime):
        return strdate.time()
    elif isinstance(strdate,datetime.time):
        return strdate
    s=get_timestr(strdate,segment)
    if s:
        fromt = datetime.datetime.strptime(strdate, s).time()
        return fromt
    return strdate
def str2datetime(strdate,segment="-"):
    """

    Args:
        strdate: 支持 YYYY，yyyymm,yyyy-m,yyyymmdd
        segment:

    Returns:

    """
    if isinstance(strdate,datetime.datetime):
        return strdate
    strdate0=strdate.strip()
    if segment in strdate0:
        if len(strdate0) == 10:
            s = "%Y" + segment + "%m" + segment + "%d"
        elif len(strdate0) == 7:
            s = "%Y" + segment + "%m"
        else:
            mid=strdate0[10]
            s = "%Y" + segment + "%m" + segment + "%d"
            if is_int4str(mid):
                hms = get_timestr(strdate0[10:])
                s=s+hms
            else:
                hms=get_timestr(strdate0[11:])
                s = s + mid + hms
        fromt = datetime.datetime.strptime(strdate0, s)
    else:
        if len(strdate0)==8:
            fromt=datetime.datetime.strptime(strdate0,"%Y%m%d")
        elif len(strdate0)==6:
            fromt = datetime.datetime.strptime(strdate0, "%Y%m")
        elif len(strdate0)==4:
            fromt = datetime.datetime.strptime(strdate0, "%Y")
        else:
            mid=strdate0[8]
            if is_int4str(mid):
                hms = get_timestr(strdate0[8:])
                ss="%Y%m%d"+hms
            else:
                hms=get_timestr(strdate0[9:])
                ss = "%Y%m%d" + mid + hms
            fromt = datetime.datetime.strptime(strdate0,ss)

    return fromt
def time2datetime(timestr,date=None):
    if date:
        now=str2datetime(date)
    else:
        now=datetime.datetime.now()
    a=timestr.split(":")
    for i in a:
        if i[0]=="0":
            i=i[1:]
    if len(a)==1:
        d=datetime.datetime(now.year,now.month,now.day,int(a[0]))
    elif len(a)==2:
        d = datetime.datetime(now.year, now.month, now.day, int(a[0]),int(a[1]))
    else:
        d = datetime.datetime(now.year, now.month, now.day,int(a[0]),int(a[1]),int(a[2]))
    return d
if __name__ == '__main__':
    print(str2datetime("2001"))
    print(str2datetime("200102"))
    print(str2datetime("20010301"))
    print(str2datetime("2001-04"))
    print(str2datetime("2001-05-01"))
    print(str2datetime("2001_06_01","_"))
    print(str2datetime("20010101 09"))
    print(str2datetime("20010101 0909"))
    print(str2datetime("20010101 090909"))
    print(str2datetime("20010101 09:09"))
    print(str2datetime("20010101 09:09:09"))
    print(str2datetime("20010101 09:09"))
    print(str2datetime("20010101T09:09:09"))
    print(str2datetime("2001-01-01 09:09:09"))